<?php
require_once("includes/pagetop.php");

if(isset($_COOKIE['Allyfolio_User']))
{
    $_SESSION['Allyfolio_User_ID']=$_COOKIE['Allyfolio_User'];
    redirect("home.php");
}

if(isset($_REQUEST['actcode']) && $_REQUEST['actcode']!="")
{
    $userinfoqry="select User_ID from af_users where Verify_Random_String=:Verify_Random_String";
    $userinfo=$pdodbobj->fetch_array($pdodbobj->query($userinfoqry,array(":Verify_Random_String"=>$_REQUEST['actcode'])));
    
    if(count($userinfo)==0)
    {
        $errormsg="Please use correct link to reset your password.";
    }
}
else
{
    $errormsg="Please use correct link to reset your password.";
}

if(isset($Password) && $Password!="")
{
    $Password=sanitize($Password);
    
    $userinfoqry="select User_ID from af_users where Verify_Random_String=:Verify_Random_String";
    $userinfo=$pdodbobj->fetch_array($pdodbobj->query($userinfoqry,array(":Verify_Random_String"=>$_REQUEST['activecode'])));
    
    
    $updatearr=array("Verify_Random_String"=>"","Password"=>md5($_POST['Password']));
    $pdodbobj->updateData("af_users",$updatearr,array("User_ID"=>$userinfo[0]['User_ID']));
    
    if($pdodbobj->affectedrows>0)
    {
        $successmsg="Your account password is reset successfully.";
    }
    else
    {
        $errormsg="System cannot reset your password due to any reason please contact to support.";
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AllyFolio | Reset Password </title>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"  />

<script src="js/general.js"></script>

<link rel="stylesheet" href="alertify/themes/alertify.core.css" />
<link rel="stylesheet" href="alertify/themes/alertify.default.css" id="toggleCSS" />
<script src="alertify/lib/alertify.min.js"></script>	
<script src="js/jquery1.10.2.min.js"></script>

</head>

<body>
	<div id="main_wrapper">
    <?php
    $pageheading="Reset Password";
    require_once("includes/header.php");
    ?>
    <!--=================Body Contain Start=======================-->
    
    <?php
                if(isset($errormsg) && $errormsg!="")
                {
                ?>
                <div class="header errormsg">
                    <?php echo $errormsg; ?>
                    
                </div>
                <?php
                }
                if(isset($successmsg) && $successmsg!="")
                {
                ?>
                <div class="header successmsg">
                    <?php echo $successmsg; ?>
                    
                </div>
                <?php
                }
                ?>
    <?php
    if(isset($errormsg) || isset($successmsg))
    {
        ?>
        <div class="wrapper row2">
        <?php
    }
    else
    {
        ?>
        <div class="wrapper row2" style="margin-top:30px;">
        <?php
    }
    
    ?>            
      	<div class="header">
    		<div class="front-signin js-front-signin">
                <form class="signin" method="post" name="resetpass">
                
                <div class="placeholding-input username">
                    
                    <label class="placeholder" for="signin-email" style="text-shadow: 0px 2px 5px #666">Password</label>
                    <input class="text-input" type="password" name="Password" id="password" style="margin-left:4px;"  />
                </div>
                
                <div class="clearfix"></div>
                
                <div class="placeholding-pwd">
                    <label class="placeholder" for="signin-password" style="text-shadow: 0px 2px 5px #666">Confirm Password</label>
                    <input class="text-input" type="password" id="cpassword" style="margin-left:4px;" />
                    
                </div>
                
                <div class="clearfix"></div>
                <button class="submit" id="login" type="submit">Sumbit</button>
                <input type="hidden" name="activecode" value="<?php echo $_REQUEST['actcode']; ?>" />
                </form>
            </div>
		</div>
    </div>
    
    <div class="clearfix"></div>
    <!--=================Body Contain end=======================-->
    </div>
<?php
    require_once("includes/footer.php");
?>
<script lang="javascript">
//<![CDATA[
    
    $("#login").click(validateResetpassInput);
    
    function validateResetpassInput()
    {
        
        if(trim(document.getElementById('password').value)=="")
        {
            alertify.alert("Please enter password.");
            document.getElementById('password').focus();
            return false;
        }
        else if(trim(document.getElementById('password').value).length<6)
        {
            alertify.alert("Password length should be at least 6 characters.");
            document.getElementById('password').focus();
            return false;
        }
        else if(trim(document.getElementById('cpassword').value)!=trim(document.getElementById('password').value))
        {
            alertify.alert("Confirm Password Mismatched.");
            document.getElementById('cpassword').focus();
            return false;
        }
        else
        {
            document.resetpass.submit();
        }
    }
    
//]]>

</script>